package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class UserApilmpl implements UserApi {

    @Autowired
    private UserMapper userMapper;


    //通过手机号获取对象
    @Override
    public User findByMobile(String mobile) {
        QueryWrapper<User> qw = new QueryWrapper();
        qw.eq("mobile", mobile);
        return userMapper.selectOne(qw);
    }

    //添加用户到数据库
    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    //修改手机号
    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }


    /**
     * 更新表中环信字段
     * @param user
     */
    @Override
    public void updateHunXin(User user) {
        userMapper.updateById(user);
    }

    /**
     * 通过id获取当前用户环信账号密码
     * @return
     * @param userId
     */
    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 根据环信id获取用户数据
     * @param huanxinId
     * @return
     */
    @Override
    public User findByHuanXin(String huanxinId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("hx_user", huanxinId);
        return userMapper.selectOne(qw);
    }


    /**
     * 统计用户的数量
     * @return
     */
    @Override
    public Integer getCount() {
        Integer integer = userMapper.selectCount(null);
        return integer;
    }

}
